<template>
  <div>
    <van-nav-bar placeholder title="黑马头条" />
    <van-form @submit="onSubmit">
      <van-field
        required
        :name="form.mobile"
        v-model="form.mobile"
        label="手机号"
        placeholder="手机号"
        :rules="[
          {
            required: true,
            message: '请填写正确的手机号',
            pattern: /^1[3-9]\d{9}$/,
            trigger: 'onChange',
          },
        ]"
      />
      <van-field
        :name="form.code"
        required
        v-model="form.code"
        type="password"
        label="验证码"
        placeholder="验证码"
        :rules="[
          {
            required: true,
            message: '请填写6位验证码',
            pattern: /^\d{6}$/,
            trigger: 'onChange',
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
        :disabled="loginBtn"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Notify } from 'vant'
import { LoginApi } from '@/api'
import { setToken } from '@/utils/token'
import Vue from 'vue'
Vue.use(Notify)
export default {
  data () {
    return {
      form: {
        mobile: '18777777777',
        code: '246810'
      },
      loginBtn: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.loginBtn = true
      try {
        // console.log('submit', values)
        const res = await LoginApi({ ...this.form })
        // 设置token
        // console.log(res.data.data.token)
        setToken(res.data.data.token)
        // console.log('123456', getToken())
        // 成功通知
        Notify({ type: 'success', message: '登录成功' })
        this.$router.push('/layout')
        // console.log(res)
        this.loginBtn = false
      } catch (err) {
        console.log('登录失败,错误如下', err)
        // 失败通知
        Notify({ type: 'danger', message: '登录失败' })
        this.loginBtn = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>>
